package Client;

import Message.AccountSendMessageInfo;
import utils.AccountSendMessageInfoList;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

public class FriendPanel extends JPanel implements MouseListener {
	MainUI mainUI;
	/**带滚动条的界面用以放置好友列表*/
	JScrollPane spFriend;
	/**显示好友列表的JPanel，要加入spFriend里*/
	private JPanel mainPanel;
	/**用户的好友标签数组*/
	JLabel []friendList;
	/**用户的好友id列表*/
	List<String> friendIdList;


	FriendPanel(MainUI mainUI){
		this.mainUI=mainUI;
		//初始化好友
		friendIdList=mainUI.user.getFriendIdList();//得到用户的好友列表
		this.setLayout(new BorderLayout());
		mainPanel=new JPanel();
		spFriend=new JScrollPane(mainPanel);
		this.add(spFriend,"Center");
		int num=friendIdList.size();
		mainPanel.setLayout(new GridLayout(0,1));

		friendList=new JLabel[num];
		for(int i=num-1;i>=0;i--){
			friendList[i]=new JLabel(friendIdList.get(i));
			friendList[i].setIcon(new ImageIcon("src/image/init_head.jpg"));
			friendList[i].addMouseListener(this);
			mainPanel.add(friendList[i]);
		}
		this.setVisible(true);
	}

	public void getNewMessage(){
		AccountSendMessageInfoList accountSendMessageInfoList=mainUI.user.getMessageList();
		for (int i = 0; i < accountSendMessageInfoList.size(); i++) {
			if (mainUI.user.getMessageListElementLength(i) > mainUI.user.getMessageOneListLength(i)){
				for(int j=0;j<friendIdList.size();j++){
					if(accountSendMessageInfoList.get(i).get(0).getUserIdInDatabaseTo().equals(friendIdList.get(i))){
						friendList[i].setBorder(BorderFactory.createLineBorder(Color.yellow));
					}
				}
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getClickCount()==2){
			JLabel jLabel=((JLabel)e.getSource());
			String friendId=jLabel.getText().trim();
			if(!mainUI.user.haveThisChatUI((mainUI.user.getUserId()+friendId))){//该用户的聊天窗口列表中没有和这个好友的聊天窗口
				jLabel.setBorder(BorderFactory.createEmptyBorder());
				new ChatUI(mainUI.user,mainUI.user.getUserId(),friendId);
			}
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		JLabel jLabel=(JLabel)e.getSource();
		jLabel.setBorder(BorderFactory.createLineBorder(Color.red));
		jLabel.setForeground(Color.red);
	}

	@Override
	public void mouseExited(MouseEvent e) {
		JLabel jLabel=(JLabel) e.getSource();
		jLabel.setForeground(Color.black);
		jLabel.setBorder(BorderFactory.createEmptyBorder());
	}
}
